﻿
// TextView.cpp: CTextView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Text.h"
#endif

#include "TextDoc.h"
#include "TextView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTextView

IMPLEMENT_DYNCREATE(CTextView, CView)

BEGIN_MESSAGE_MAP(CTextView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_WM_CREATE()
	ON_WM_CHAR()
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
END_MESSAGE_MAP()

// CTextView 构造/析构

CTextView::CTextView() noexcept
{
	// TODO: 在此处添加构造代码
	m_strLine = "";
	m_ptOrigin = 0;
	m_nWidth = 0;
}

CTextView::~CTextView()
{
}

BOOL CTextView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CTextView 绘图

void CTextView::OnDraw(CDC* pDC)
{
	CTextDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	CString str(" 字符串直接构造");
	pDC->TextOut(50, 50, str);

	//str.LoadString(IDS_MYSTR);
	//pDC->TextOut(200, 200, str);
	CSize sz = pDC->GetTextExtent(str);

	// 通过这种路径的方式可以使得画出来的图像在即使有其它网格线条的情况下也可以无干扰绘制
	pDC->BeginPath();
	pDC->Rectangle(50, 50, 50 + sz.cx, 50 + sz.cy);
	pDC->EndPath();

	pDC->SelectClipPath(RGN_DIFF);// 通过路径程可以使得输出的区域通过求交求差等显示
	// 画网格
	for (int i = 0; i < 300; i += 10)
	{
		pDC->MoveTo(0, i);
		pDC->LineTo(300, i);
		pDC->MoveTo(i, 0);
		pDC->LineTo(i, 300);
	}

	// TODO: 在此处为本机数据添加绘制代码
}


// CTextView 打印

BOOL CTextView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CTextView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CTextView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CTextView 诊断

#ifdef _DEBUG
void CTextView::AssertValid() const
{
	CView::AssertValid();
}

void CTextView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTextDoc* CTextView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextDoc)));
	return (CTextDoc*)m_pDocument;
}
#endif //_DEBUG


// CTextView 消息处理程序


int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CClientDC dc(this);
	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);// 获取当前字体的信息

	//CreateSolidCaret(tm.tmAveCharWidth / 8, tm.tmHeight);
	//ShowCaret();// 文本插入符

	bitmap.LoadBitmap(IDB_BITMAP1);
	CreateCaret(&bitmap);
	ShowCaret();

	/*
		设置定时器
	*/
	SetTimer(1, 100, nullptr);

	return 0;
}


void CTextView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CClientDC dc(this);

	// 设置字体
	CFont font;
	font.CreatePointFont(300, "宋体", nullptr);
	CFont* pOldFont = dc.SelectObject(&font);

	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);
	if (0x0d == nChar) {
		m_strLine.Empty();
		m_ptOrigin.y += tm.tmHeight;
	}
	else if (0x08 == nChar) {
		COLORREF clr = dc.SetTextColor(dc.GetBkColor());
		dc.TextOut(m_ptOrigin.x, m_ptOrigin.y, m_strLine);
		m_strLine = m_strLine.Left(m_strLine.GetLength() - 1);
		dc.SetTextColor(clr);
	}
	else {
		m_strLine += (char)nChar;
	}
	CSize  sz = dc.GetTextExtent(m_strLine);

	CPoint pt;
	pt.x = m_ptOrigin.x + sz.cx;
	pt.y = m_ptOrigin.y;
	SetCaretPos(pt); // 设置输入符的位置

	dc.TextOut(m_ptOrigin.x, m_ptOrigin.y, m_strLine);// 输出文本

	dc.SelectObject(pOldFont); // 还原字体

	CView::OnChar(nChar, nRepCnt, nFlags);
}


void CTextView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	SetCaretPos(point);// 设置插入符的位置
	m_strLine.Empty();
	m_ptOrigin = point;
	CView::OnLButtonDown(nFlags, point);
}


void CTextView::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (1 == nIDEvent) {
		m_nWidth += 5;
		CClientDC dc(this);
		TEXTMETRIC tm;
		dc.GetTextMetrics(&tm);
		CRect rect;
		rect.left = 0;
		rect.top = 200;
		rect.right = m_nWidth;
		rect.bottom = rect.top + tm.tmHeight;

		dc.SetTextColor(RGB(255, 0, 0));
		CString str;
		str.LoadStringA(IDS_MYSTR);
		dc.DrawText(str, rect, DT_LEFT);
		rect.top = 150;
		rect.bottom = rect.top + tm.tmHeight;
		dc.DrawText(str, rect, DT_RIGHT);

		CSize sz = dc.GetTextExtent(str);
		if (m_nWidth > sz.cx) {
			m_nWidth = 0;
			dc.SetTextColor(RGB(0, 255, 0));
			dc.TextOut(0, 200, str);
		}
	}

	CView::OnTimer(nIDEvent);
}
